package com.kawins.spring.valid.annotation.impl;

import com.kawins.spring.valid.annotation.EqualsNum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author rankai
 *         createTime 2017-06-2017/6/15 17:55
 */
public class EqualsNumImpl implements ConstraintValidator<EqualsNum, Integer> {

    private int[] values;

    @Override
    public void initialize(EqualsNum constraintAnnotation) {
        this.values = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        } else {
            Boolean flag = Boolean.FALSE;
            for (int i = 0; i < values.length; i++) {
                if (value.equals(values[i])) {
                    flag = Boolean.TRUE;
                    break;
                }
            }
            return flag;
        }
    }
}
